package com.example.doubt.activity.lifecycle;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.view.View;

import com.example.doubt.R;
import com.mxin.common.log.LogA;
import com.mxin.util.AppUtil;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Dialog对Activity生命周期的影响
 *
 */
public class DialogLifecycleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_lifecycle);
    }


    /**
     * 弹出Dialog时，对Activity的生命周期没有影响，还是onResumed
     * 此时按Home键时，Activity的生命周期执行onPaused、onStopped、onSaveInstanceState
     * 返回应用时，执行onStarted、OnResumed
     * @param view
     */
    public void showDialog(View view) {
        new AlertDialog.Builder(this)
                .setMessage("弹出框测试Activity的生命周期")
                .create().show();
    }

    public void registerActivityLifeCycle(View v){
        AppUtil.getApp().registerActivityLifecycleCallbacks(lifecycleCallback);
    }

    public void unRegisterActivityLifeCycle(View v){
        AppUtil.getApp().unregisterActivityLifecycleCallbacks(lifecycleCallback);
    }

    Application.ActivityLifecycleCallbacks lifecycleCallback =
    new Application.ActivityLifecycleCallbacks(){

        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
            LogA.i(activity.getClass().getSimpleName() +"  =>  onCreated ");
        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {
            LogA.i(activity.getClass().getSimpleName() +"  =>  onStarted ");
        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {
            LogA.i(activity.getClass().getSimpleName() +"  =>  onResumed ");
        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {
            LogA.i(activity.getClass().getSimpleName() +"  =>  onPaused ");
        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {
            LogA.i(activity.getClass().getSimpleName() +"  =>  onStopped ");
        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
            LogA.i(activity.getClass().getSimpleName() +"  =>  onSaveInstanceState ");
        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {
            LogA.i(activity.getClass().getSimpleName() +"  =>  onDestroyed ");
        }
    };
}